﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NextInventory.Common;
using NextInventory.Data;
using NextInventory.Model.User;

namespace NextInventory.Business.EntityRepositories
{
    public interface IUserRepository : IBaseRepository
    {
        #region User

        UserModel GetUserByID(string Id);
        List<UserModel> GetAllUsers();
        List<UserModel> GetUsersByRoleID(int Id);

        bool AddUser(RegisterModel model, out string message);
        bool UpdateUser(UserModel model);
        bool DeleteUser(int Id);
        bool ValidateUser(LoginModel model);
        bool ChangePassword(ChangePasswordModel model, out string message);
        bool ForgetPassword(UserModel model);

        #endregion User

        #region User Role

        #endregion User Role

        #region User Right

        #endregion User Right
    }

    public class UserRepository : BaseRepository, IUserRepository
    {
        #region User

        public UserModel GetUserByID(string Id)
        {
            //Query Syntax
            //return (from u in _Contaxt.Users
            //        where u.UserId == Id
            //        select new UserModel
            //        {

            //            FirstName = u.FirstName,
            //            LastName = u.LastName,
            //            Username = u.Username,
            //            Email = u.Email,

            //        }).FirstOrDefault();

            //Method Syntax
            return _Contaxt
                    .Users
                    .Select(u => new UserModel
                    {

                        FirstName = u.FirstName,
                        LastName = u.LastName,
                        Username = u.Username,
                        Email = u.Email,
                    })
                    .FirstOrDefault(o => o.Username == Id);
        }

        public List<UserModel> GetAllUsers()
        {
            return (_Contaxt
                    .Users
                    .Select(u => new UserModel
                    {

                        FirstName = u.FirstName,
                        LastName = u.LastName,
                        Username = u.Username,
                        Email = u.Email,
                    })).ToList();
        }

        public List<UserModel> GetUsersByRoleID(int Id)
        {
            return (_Contaxt
                    .Users
                    .Select(u => new UserModel
                    {

                        FirstName = u.FirstName,
                        LastName = u.LastName,
                        Username = u.Username,
                        Email = u.Email,
                    })).ToList();
        }

        public bool AddUser(RegisterModel model, out string message)
        {
            NextInventoryContext context = new NextInventoryContext();
            // Validate
            if (string.IsNullOrWhiteSpace(model.UserName))
            {
                message = "Username is required.";
                return false;
            }

            if (string.IsNullOrWhiteSpace(model.Email))
            {
                message = "Email address is required.";
                return false;
            }

            if (string.IsNullOrWhiteSpace(model.Password))
            {
                message = "Password is required.";
                return false;
            }

            if (context.Users.FirstOrDefault(o => o.Username == model.UserName) != null)
            {
                message = "Username is already registered.";
                return false;
            }

            if (context.Users.FirstOrDefault(o => o.Username == model.Email) != null)
            {
                message = "Email address is already registered.";
                return false;
            }

            model.UserStatus = UserStatus.Active;

            // Initailize
            User user = new User()
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                Email = model.Email,
                Username = model.UserName,
                Password = model.Password,
                IsActive = Convert.ToBoolean(UserStatus.Active),
                IsLocked = Convert.ToBoolean(UserLocked.Unlocked)

            };


            // Save
            context.Users.Add(user);
            context.SaveChanges();

            if (user.UserId > 0)
            {
                message = "User added successfully.";
                return true;
            }
            else
            {
                message = "Error accure during register new user.";
                return false;
            }
        }

        public bool UpdateUser(UserModel model)
        {
            throw new NotImplementedException();
        }

        public bool DeleteUser(int Id)
        {
            throw new NotImplementedException();
        }

        public bool ValidateUser(LoginModel model)
        {
            NextInventoryContext context = new NextInventoryContext();

            var login = (from u in context.Users
                         where u.Username == model.UserName && u.Password == model.Password
                         select u).FirstOrDefault();

            if (login != null)
                return true;

            return false;
        }

        public bool ChangePassword(ChangePasswordModel model, out string message)
        {
            bool rtn = false;
            // Validate 

            if (string.IsNullOrWhiteSpace(model.OldPassword))
            {
                message = "Old Password is required";
                return rtn;
            }

            if (string.IsNullOrWhiteSpace(model.NewPassword))
            {
                message = "New password is required";
                return rtn;
            }

            if (string.IsNullOrWhiteSpace(model.ConfirmPassword))
            {
                message = "Confirm password is required";
                return rtn;
            }

            if (string.IsNullOrWhiteSpace(model.NewPassword) != string.IsNullOrWhiteSpace(model.ConfirmPassword))
            {
                message = "Password and Confirm password did not macth.";
                return rtn;
            }
            using (NextInventoryContext context = new NextInventoryContext())
            {
                User user = context.Users.FirstOrDefault(o => o.Username == model.Username);

                if (user == null)
                {
                    message = "User could not found for change password.";
                    return rtn;
                }

                try
                {
                    user.Password = model.NewPassword;

                    context.SaveChanges();

                    message = "Password successfuly changed.";
                    rtn = true;
                }
                catch (Exception ex)
                {
                    rtn = false;
                    throw;
                }
            }
            return rtn;

        }

        public bool ForgetPassword(UserModel model)
        {
            throw new NotImplementedException();
        }

        #endregion User
    }
}
